//
//  NewsDetailViewController.m
//  CarLife
//
//  Created by yu on 2018/1/8.
//  Copyright © 2018年 jpas. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "AFNetworking.h"
@interface NewsDetailViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation NewsDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setWhiteNavigationBar];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self setDefaultNavigationBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"资讯详情";
    [self setLeftImageNamed:@"back_gray" action:@selector(returnAction)];
    [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    [PPNetworkHelper setResponseSerializer:PPResponseSerializerHTTP];
    [PPNetworkHelper GET:self.url parameters:nil success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:KEY_WINDOW];
        NSString *html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        [self deleteAD:html];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:KEY_WINDOW];
    }];

//    NSString *url = @"https://m.toutiao.com/";
//    NSURL *URL = [[NSURL alloc] initWithString:url];
//    [self.webView loadRequest:[NSURLRequest requestWithURL:URL]];
}

- (void)deleteAD:(NSString *)html {
//    html = [str stringByReplacingOccurrencesOfString:@"<div id=\"J_hot_news\" class=\"hot-news\"></div>" withString:@""];
//    [self.webView loadHTMLString:html baseURL:nil];
    NSString *pattern = @"<div id=\"news_check\">[\\s\\S]*</script>";
    NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    html = [expression stringByReplacingMatchesInString:html options:NSMatchingReportCompletion range:NSMakeRange(0, html.length) withTemplate:@""];
    [self.webView loadHTMLString:html baseURL:nil];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
//    PPLog(@"web html == %@",currentURL);
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '80%'"];

//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'red'"];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)returnAction {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
